
package cz.czu.pef.DataModelling.tests.data.prepare;

import java.util.*;
import java.util.logging.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;

import cz.czu.pef.DataModelling.*;
import cz.czu.pef.DataModelling.domainObjects.*;
import cz.czu.pef.DataModelling.service.api.*;
import cz.czu.pef.DataModelling.tests.data.BasicData;
import cz.czu.pef.DataModelling.utils.Constants;


@Component(value = "prepareITData")
@SuppressWarnings("unused")
@ContextConfiguration(locations = {Constants.springTestingClasspathConfig})
public class PrepareITData extends BasicData
{

  Logger logger = Logger.getLogger(PrepareITData.class.getName());

  public void fillBasicData()
  {
    // ##############################################
    // EXISTING OBJECTS
    // ##############################################

    MedicalExaminationType medExam_Praktik = medicalExaminationTypeService.findByAttributeUniqueResult("code", Constants.MEDICAL_EXAMINATION_TYPE_PRAKTIK);
    MedicalExaminationType medExam_Ocni = medicalExaminationTypeService.findByAttributeUniqueResult("code", Constants.MEDICAL_EXAMINATION_TYPE_OCNI);
    MedicalExaminationType medExam_Neurolog = medicalExaminationTypeService.findByAttributeUniqueResult("code", Constants.MEDICAL_EXAMINATION_TYPE_NEURO);
    MedicalExaminationType medExam_Psycholog = medicalExaminationTypeService.findByAttributeUniqueResult("code", Constants.MEDICAL_EXAMINATION_TYPE_PSYCHO);
    if (medExam_Praktik == null || medExam_Ocni == null || medExam_Neurolog == null || medExam_Psycholog == null)
      throw new RuntimeException("Some MedicalExaminationType is missing!");

    TrainingType training_BOZP = trainingTypeService.findByAttributeUniqueResult("code", Constants.TRAINING_TYPE_BOZP);
    TrainingType training_PO = trainingTypeService.findByAttributeUniqueResult("code", Constants.TRAINING_TYPE_PO);
    if (training_BOZP == null || training_PO == null)
      throw new RuntimeException("Some TrainingType is missing!");

    // ##############################################
    // ROLE TYPES
    // ##############################################

    RoleType roleITArchitect = new RoleType(Constants.ROLETYPE_CODE_IT_ARCHITECT, "IT Architect", "IT Architect description");
    // MEDICAL EXAMINATION TYPES - roleITArchitect
    roleITArchitect.addMedicalExaminationTypeForRoleType(medicalExaminationTypeForRoleTypeService.merge(new MedicalExaminationTypeForRoleType(medExam_Praktik, yearsToSeconds(3))));
    roleITArchitect = roleTypeService.mergeUnique("code", Constants.ROLETYPE_CODE_IT_ARCHITECT, roleITArchitect);


    RoleType roleITProgrammer = new RoleType(Constants.ROLETYPE_CODE_IT_PROGRAMMER, "IT Programmer", "IT Programmer description");
    // MEDICAL EXAMINATION TYPES - roleITProgrammer
    roleITProgrammer.addMedicalExaminationTypeForRoleType(medicalExaminationTypeForRoleTypeService.merge(new MedicalExaminationTypeForRoleType(medExam_Praktik, yearsToSeconds(3))));
    roleITProgrammer.addMedicalExaminationTypeForRoleType(medicalExaminationTypeForRoleTypeService.merge(new MedicalExaminationTypeForRoleType(medExam_Ocni, yearsToSeconds(3))));
    roleITProgrammer = roleTypeService.mergeUnique("code", Constants.ROLETYPE_CODE_IT_PROGRAMMER, roleITProgrammer);


    RoleType roleITAdmin = new RoleType(Constants.ROLETYPE_CODE_IT_ADMINISTRATOR, "IT Admin", "IT Admin description");
    // MEDICAL EXAMINATION TYPES - roleITProgrammer
    roleITAdmin.addMedicalExaminationTypeForRoleType(medicalExaminationTypeForRoleTypeService.merge(new MedicalExaminationTypeForRoleType(medExam_Praktik, yearsToSeconds(3))));
    roleITAdmin.addMedicalExaminationTypeForRoleType(medicalExaminationTypeForRoleTypeService.merge(new MedicalExaminationTypeForRoleType(medExam_Ocni, yearsToSeconds(3))));
    roleITAdmin = roleTypeService.mergeUnique("code", Constants.ROLETYPE_CODE_IT_ADMINISTRATOR, roleITAdmin);

    RoleType roleITConsultant = new RoleType(Constants.ROLETYPE_CODE_IT_CONSULTANT, "IT Consultant", "IT Consultant description");
    // MEDICAL EXAMINATION TYPES - roleITConsultant
    roleITConsultant.addMedicalExaminationTypeForRoleType(medicalExaminationTypeForRoleTypeService.merge(new MedicalExaminationTypeForRoleType(medExam_Praktik, yearsToSeconds(3))));
    roleITConsultant.addMedicalExaminationTypeForRoleType(medicalExaminationTypeForRoleTypeService.merge(new MedicalExaminationTypeForRoleType(medExam_Psycholog, yearsToSeconds(5))));
    roleITConsultant = roleTypeService.mergeUnique("code", Constants.ROLETYPE_CODE_IT_CONSULTANT, roleITConsultant);
  }

  public void fillPeopleAndCompanies()
  {
    // NESS company
    Company nessCompany;
    if (companyService.findByAttribute("name", "NESS").isEmpty())
    {
      nessCompany = companyService.merge(new Company("NESS", "ICO 2", "DIC 2"));
    }
    else
    {
      nessCompany = companyService.findByAttributeUniqueResult("name", "NESS");
    }

    // NESS employees
    {
      Employee nessEmployee = new Employee(new Person("Petr", "Bažant", null));
      nessEmployee.addRole(new Role(roleTypeService.findByAttributeUniqueResult("code", Constants.ROLETYPE_CODE_IT_ARCHITECT)));
      nessEmployee.addRole(new Role(roleTypeService.findByAttributeUniqueResult("code", Constants.ROLETYPE_CODE_IT_PROGRAMMER)));
      nessEmployee = employeeService.merge(nessEmployee);
      nessCompany.hireEmployee(nessEmployee);
    }
    {
      Employee nessEmployee = new Employee(new Person("Tomáš", "Boch", null));
      nessEmployee.addRole(new Role(roleTypeService.findByAttributeUniqueResult("code", Constants.ROLETYPE_CODE_IT_PROGRAMMER)));
      nessEmployee = employeeService.merge(nessEmployee);
      nessCompany.hireEmployee(nessEmployee);
    }
    {
      Employee nessEmployee = new Employee(new Person("Karel", "Malec", null));
      nessEmployee.addRole(new Role(roleTypeService.findByAttributeUniqueResult("code", Constants.ROLETYPE_CODE_IT_ADMINISTRATOR)));
      nessEmployee = employeeService.merge(nessEmployee);
      nessCompany.hireEmployee(nessEmployee);
    }
    {
      Employee nessEmployee = new Employee(new Person("Martin", "Sedláček", null));
      nessEmployee.addRole(new Role(roleTypeService.findByAttributeUniqueResult("code", Constants.ROLETYPE_CODE_IT_ADMINISTRATOR)));
      nessEmployee = employeeService.merge(nessEmployee);
      nessCompany.hireEmployee(nessEmployee);
    }
    {
      Employee nessEmployee = new Employee(new Person("René", "Trbušek", null));
      nessEmployee.addRole(new Role(roleTypeService.findByAttributeUniqueResult("code", Constants.ROLETYPE_CODE_IT_CONSULTANT)));
      nessEmployee = employeeService.merge(nessEmployee);
      nessCompany.hireEmployee(nessEmployee);
    }
    {
      Employee nessEmployee = new Employee(new Person("Josef", "Pecinovský", null));
      nessEmployee.addRole(new Role(roleTypeService.findByAttributeUniqueResult("code", Constants.ROLETYPE_CODE_IT_CONSULTANT)));
      nessEmployee.addRole(new Role(roleTypeService.findByAttributeUniqueResult("code", Constants.ROLETYPE_CODE_IT_PROGRAMMER)));
      nessEmployee = employeeService.merge(nessEmployee);
      nessCompany.hireEmployee(nessEmployee);
    }
    nessCompany = companyService.merge(nessCompany);

    
    
    
    
    
    // ADRESA - Křižatky
    Address address1 = addressService.merge(new Address("Street 1", "City 1", "Country 1", "ZIP 1"));
    Address address2 = addressService.merge(new Address("Street 2", "City 2", "Country 2", "ZIP 2"));

    // Dagmar Stříbrná
    Person dasa = new Person("Dagmar", "Stříbrná", address1);
    dasa.setBirthDate(new Date());
    dasa = personService.merge(dasa);
    Employee empStribrna = new Employee(dasa);
    empStribrna.addRole(new Role(roleTypeService.findByAttributeUniqueResult("code", Constants.ROLETYPE_CODE_ZAHR_MISTR)));
    empStribrna = employeeService.merge(empStribrna);

    ExternalWorker extWorkerDasa = new ExternalWorker(dasa);
    extWorkerDasa.addRole(new Role(roleTypeService.findByAttributeUniqueResult("code", Constants.ROLETYPE_CODE_ZAHRADNIK)));
    extWorkerDasa = externalWorkerService.merge(extWorkerDasa);

    ExternalWorker extWorker1 = new ExternalWorker(personService.merge(new Person("John", "ExtWorker1", null))); // without address
    extWorker1 = externalWorkerService.merge(extWorker1);
    ExternalWorker extWorker2 = new ExternalWorker(personService.merge(new Person("John", "ExtWorker2", address2)));
    extWorker2 = externalWorkerService.merge(extWorker2);
    ExternalWorker extWorker3 = new ExternalWorker(personService.merge(new Person("John", "ExtWorker3", null))); // without address
    extWorker3 = externalWorkerService.merge(extWorker3);
    ExternalWorker extWorker4 = new ExternalWorker(personService.merge(new Person("John", "ExtWorker4", address2)));
    extWorker4 = externalWorkerService.merge(extWorker4);

    nessCompany.hireExternalWorker(extWorkerDasa);
    nessCompany.hireExternalWorker(extWorker1);
    nessCompany.hireExternalWorker(extWorker2);
    nessCompany.hireExternalWorker(extWorker3);
    nessCompany.hireExternalWorker(extWorker4);
    nessCompany = companyService.merge(nessCompany);
  }

}
